package cn.elead.chaos.shiro.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;

import cn.elead.chaos.api.enums.ExStatus;
import cn.elead.chaos.api.web.IExceptionResolverService;
import cn.elead.chaos.framework.util.ResponseUtils;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * 自定义异常处理
 */
@Slf4j
public class ShiroExceptionResolverService implements IExceptionResolverService {

	@Override
	public ExStatus doException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex, Object view) {
		if (ex instanceof UnauthorizedException) {
			log.error(ex.getMessage(), ex);
			ResponseUtils.sendFail(request, response, ErrorCodeEnum.FORBIDDEN, ex);
			return ExStatus.HIT;
		}
		if (ex instanceof AuthorizationException) {
			log.error(ex.getMessage(), ex);
			ResponseUtils.sendFail(request, response, ErrorCodeEnum.FORBIDDEN, ex);
			return ExStatus.HIT;
		}
		return ExStatus.SKIP;
	}

}
